/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "timeline_srv_stub.h"

#include "message_parcel.h"

#include "timeline_srv_proxy.h"
#include "timeline_common.h"
#include "itimeline_callback.h"
#include "itimeline_algorithm_callback.h"
#include "timeline_service.h"

namespace OHOS {
namespace Msdp {

int TimelineSrvStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_SERVICE, "cmd = %{public}d, flags = %{public}d", code, option.GetFlags());
    std::u16string descriptor = TimelineSrvStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    if (descriptor != remoteDescriptor) {
        TIMELINE_HILOGE(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::OnRemoteRequest failed, descriptor is not matched!");
        return E_TIMELINE_GET_SERVICE_FAILED;
    }

    switch (code) {
        case static_cast<int>(ITimeline::TIMELINE_SUBSCRIBE): {
            return SubscribeStub(data);
        }
        case static_cast<int>(ITimeline::TIMELINE_UNSUBSCRIBE): {
            return UnSubscribeStub(data);
        }
        case static_cast<int>(ITimeline::TIMELINE_SETPOSITIONSTATE): {
            return SetPositionStateStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_SETHOMEPOSITION): {
            return SetHomePositionStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_SETCOMPANYPOSITION): {
            return SetCompanyPositionStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_SETSLEEPTIME): {
            return SetSleepTimeStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_SETRESTTIME): {
            return SetRestTimeStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_SETWORKTIME): {
            return SetWorkTimeStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETSLEEPTIME): {
            return GetSleepTimeStub(data, reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETWAKEUPTIME): {
            return GetWakeupTimeStub(reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETSTARTRESTTIME): {
            return GetRestTimeStartStub(reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETENDRESTTIME): {
            return GetRestTimeEndStub(reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETSTARTWORKTIME): {
            return GetWorkTimeStartStub(reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETENDWORKTIME): {
            return GetWorkTimeEndStub(reply);
        }
        case static_cast<int>(ITimeline::TIMELINE_GETFORCECAST): {
            return GetForcecastStub(data, reply);
        }
        default: {
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
        }
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::SubscribeStub(MessageParcel& data)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_SERVICE, "Enter");
    int32_t area = 0;
    TIMELINE_READ_PARCEL_WITH_RET(data, Int32, area, E_TIMELINE_READ_PARCEL_ERROR);
    sptr<IRemoteObject> obj = data.ReadRemoteObject();
    TIMELINE_RETURN_IF_WITH_RET((obj == nullptr), E_TIMELINE_READ_PARCEL_ERROR);
    sptr<ITimelineCallback> callback = iface_cast<ITimelineCallback>(obj);
    TIMELINE_RETURN_IF_WITH_RET((callback == nullptr), E_TIMELINE_READ_PARCEL_ERROR);
    Subscribe(TimelineDataUtils::TimelineArea(area), callback);
    return ERR_OK;
}

int32_t TimelineSrvStub::UnSubscribeStub(MessageParcel& data)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_SERVICE, "Enter");
    int32_t area = 0;
    TIMELINE_READ_PARCEL_WITH_RET(data, Int32, area, E_TIMELINE_READ_PARCEL_ERROR);
    sptr<IRemoteObject> obj = data.ReadRemoteObject();
    TIMELINE_RETURN_IF_WITH_RET((obj == nullptr), E_TIMELINE_READ_PARCEL_ERROR);
    sptr<ITimelineCallback> callback = iface_cast<ITimelineCallback>(obj);
    TIMELINE_RETURN_IF_WITH_RET((callback == nullptr), E_TIMELINE_READ_PARCEL_ERROR);
    UnSubscribe(TimelineDataUtils::TimelineArea(area), callback);
    return ERR_OK;
}

int32_t TimelineSrvStub::SetPositionStateStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::SetPositionStateStub : fun is start");
    int32_t homevalue = 0;
	int32_t companyvalue = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Int32, homevalue, E_TIMELINE_READ_PARCEL_ERROR);
	TIMELINE_READ_PARCEL_WITH_RET(data, Int32, companyvalue, E_TIMELINE_READ_PARCEL_ERROR);
																  

    bool ret = SetPositionState(homevalue, companyvalue);
    if (!reply.WriteBool(ret)) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "Failed to write SetPositionState return value");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::SetHomePositionStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::SetHomePosition : fun is start");
    double longitude = 0;
	double latitude = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Double, longitude, E_TIMELINE_READ_PARCEL_ERROR);
	TIMELINE_READ_PARCEL_WITH_RET(data, Double, latitude, E_TIMELINE_READ_PARCEL_ERROR);
																  

    bool ret = SetHomePosition(longitude, latitude);
    if (!reply.WriteBool(ret)) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "Failed to write SetHomePosition return value");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::SetCompanyPositionStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::SetCompanyPosition : fun is start");
    double longitude = 0;
	double latitude = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Double, longitude, E_TIMELINE_READ_PARCEL_ERROR);
	TIMELINE_READ_PARCEL_WITH_RET(data, Double, latitude, E_TIMELINE_READ_PARCEL_ERROR);
																  

    bool ret = SetCompanyPosition(longitude, latitude);
    if (!reply.WriteBool(ret)) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "Failed to write SetCompanyPosition return value");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::SetSleepTimeStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::SetSleepTime : fun is start");
    int32_t sleeptime = 0;
	int32_t wakeuptime = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Int32, sleeptime, E_TIMELINE_READ_PARCEL_ERROR);
	TIMELINE_READ_PARCEL_WITH_RET(data, Int32, wakeuptime, E_TIMELINE_READ_PARCEL_ERROR);
																  

    bool ret = SetSleepTime(sleeptime, wakeuptime);
    if (!reply.WriteBool(ret)) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "Failed to write SetSleepTime return value");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::SetRestTimeStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::SetRestTime : fun is start");
    int32_t resttimestart = 0;
	int32_t resttimeend = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Int32, resttimestart, E_TIMELINE_READ_PARCEL_ERROR);
	TIMELINE_READ_PARCEL_WITH_RET(data, Int32, resttimeend, E_TIMELINE_READ_PARCEL_ERROR);
																  

    bool ret = SetRestTime(resttimestart, resttimeend);
    if (!reply.WriteBool(ret)) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "Failed to write SetRestTime return value");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::SetWorkTimeStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::SetWorkTime : fun is start");
    int32_t worktimestart = 0;
	int32_t worktimeend = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Int32, worktimestart, E_TIMELINE_READ_PARCEL_ERROR);
	TIMELINE_READ_PARCEL_WITH_RET(data, Int32, worktimeend, E_TIMELINE_READ_PARCEL_ERROR);
																  

    bool ret = SetWorkTime(worktimestart, worktimeend);
    if (!reply.WriteBool(ret)) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "Failed to write SetWorkTime return value");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetSleepTimeStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetSleepTimeStub : fun is start");
    double longitude = 0;

    TIMELINE_READ_PARCEL_WITH_RET(data, Double, longitude, E_TIMELINE_READ_PARCEL_ERROR);

    uint32_t ret = 0;
    ret = GetSleepTime();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetSleepTimeStub, SleepTime = %{public}u.", ret);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get Sleep Time Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetWakeupTimeStub(MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetWakeupTimeStub : fun is start");
    uint32_t ret = 0;
    ret = GetWakeupTime();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetWakeupTimeStub, WakeupTime = %{public}u.", ret);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get WakeUP Time Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetRestTimeStartStub(MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetRestTimeStartStub : fun is start");
    uint32_t ret = 0;
    ret = GetRestTimeStart();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetRestTimeStartStub, RestStartTime = %{public}u.", ret);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get Rest Start Time Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetRestTimeEndStub(MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetRestTimeEndStub : fun is start");
    uint32_t ret = 0;
    ret = GetRestTimeEnd();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetRestTimeEndStub, RestEndTime = %{public}u.", ret);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get Rest End Time Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetWorkTimeStartStub(MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetWorkTimeStartStub : fun is start");
    uint32_t ret = 0;
    ret = GetWorkTimeStart();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetWorkTimeStartStub, WorkStartTime = %{public}u.", ret);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get Work Start Time Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetWorkTimeEndStub(MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetWorkTimeEndStub : fun is start");
    uint32_t ret = 0;
    ret = GetWorkTimeEnd();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetWorkTimeEndStub, WorkEndTime = %{public}u.", ret);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get Work EndTime Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}

int32_t TimelineSrvStub::GetForcecastStub(MessageParcel& data, MessageParcel& reply)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub::GetForcecastStub : fun is start");
    uint32_t time = 0;
    TIMELINE_READ_PARCEL_WITH_RET(data, Uint32, time, E_TIMELINE_READ_PARCEL_ERROR);
    TimelineDatabaseInfo::ForcecastPosition  ret = GetForcecast(time);
    if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "TimelineSrvStub:: Get Forcecast Fail!");
        return E_TIMELINE_READ_PARCEL_ERROR;
    }
    return ERR_OK;
}
} // Msdp
} // OHOS
